package com.cyq.day25.list;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Book {
    private String bookName;
    private int price;
    private String authorNmae;
    private int bookNum;
    static ArrayList<Book> arrayList=new ArrayList<Book>();
    static Scanner scanner=new Scanner(System.in);

    public Book(String bookName, int price, String authorNmae, int bookNum) {
        this.bookName = bookName;
        this.price = price;
        this.authorNmae = authorNmae;
        this.bookNum = bookNum;
    }

    public static void init(){
        Book b1=new Book("星辰变",50,"西红柿",1);
        Book b2=new Book("斗破苍穹",60,"土豆",2);
        Book b3=new Book("123",70,"456",3);
        arrayList.add(b1);
        arrayList.add(b2);
        arrayList.add(b3);


    }

    public static void main(String[] args) {
        init();
        while (true){
        try {
            BookMenu();
        }catch (Exception e){

        }

        }
    }

    private static void BookMenu() {
        String s = JOptionPane.showInputDialog(null, "请选择所需操作"+"\n"+"1.添加图书"+"\n"
                                                                                                  +"2.删除图书"+"\n"
                                                                                                  +"3.查找图书"+"\n"
                                                                                                  +"4.修改图书"+"\n"
                                                                                                  +"5.展示图书"+"\n"
                                                                                                  +"6.退出");
        if (s.equals("1")){
            addBook();
        }else if (s.equals("2")){
            deleteBook();
        }
        else if (s.equals("3")){
            searchBook();
        }else if (s.equals("4")){
            changeBook();
        }
        else if (s.equals("5")){
            show();
        }
        else if (s.equals("6")){
            System.exit(0);
        }

    }

    public static void addBook(){
        String inputBookname = JOptionPane.showInputDialog(null, "请输入添加的图书名");
        String inputPrice = JOptionPane.showInputDialog(null, "请输入添加的图书价格");
        String inputAuthorName=JOptionPane.showInputDialog(null,"请输入添加的图书作者名");
        String inputBookNum = JOptionPane.showInputDialog(null, "请输入添加的图书编号");
        arrayList.add(new Book(inputBookname,Integer.parseInt(inputPrice),inputAuthorName,Integer.parseInt(inputBookNum)));
        JOptionPane.showMessageDialog(null,"添加成功");
    }
    public static void deleteBook(){
        String s=JOptionPane.showInputDialog(null,"请输入你想要删除的方式"+"\n"+
                "1.按照图书名"+"\n"+
                "2.按照图书价格"+"\n"+
                "3.按照图书作者"+"\n"+
                "4.按照图书编号");
        if ("1".equals(s)){
            String inputBookName=JOptionPane.showInputDialog(null,"请输入要删除的图书名");
            for (int i = 0; i <arrayList.size() ; i++) {
                if (arrayList.get(i).bookName.equals(inputBookName)){
                    arrayList.remove(i);
                }
            }
            JOptionPane.showMessageDialog(null,"删除成功");
        }
        if ("2".equals(s)){
            String inputBookPrice=JOptionPane.showInputDialog(null,"请输入要删除的图书价格");
            for (int i = 0; i <arrayList.size() ; i++) {
                if (arrayList.get(i).price==Integer.parseInt(inputBookPrice)){
                    arrayList.remove(i);
                }
            }
            JOptionPane.showMessageDialog(null,"删除成功");

        }if ("3".equals(s)){
            String inputAuthorName=JOptionPane.showInputDialog(null,"请输入要删除的图书作者名");
            for (int i = 0; i <arrayList.size() ; i++) {
                if (arrayList.get(i).authorNmae.equals(inputAuthorName)){
                    arrayList.remove(i);
                }
            }
            JOptionPane.showMessageDialog(null,"删除成功");
        }if ("4".equals(s)){
            String inputBookNum=JOptionPane.showInputDialog(null,"请输入要删除的图书编号");
            for (int i = 0; i <arrayList.size() ; i++) {
                if (arrayList.get(i).bookNum==Integer.parseInt(inputBookNum)){
                    arrayList.remove(i);
                }
            }
            JOptionPane.showMessageDialog(null,"删除成功");
        }

    }
    public  static void searchBook(){
        int index = 0;
        String s=JOptionPane.showInputDialog(null,"请输入你想要查找的方式"+"\n"+
                                                                             "1.按照图书名"+"\n"+
                                                                             "2.按照图书价格"+"\n"+
                                                                             "3.按照图书作者"+"\n"+
                                                                             "4.按照图书编号");
        if ("1".equals(s)){
            String inputBookName=JOptionPane.showInputDialog(null,"请输入要查找的图书名");
            for (int i = 0; i <arrayList.size() ; i++) {
                if (arrayList.get(i).bookName.equals(inputBookName)){
                    index=i;
                }
            }
            JOptionPane.showMessageDialog(null,arrayList.get(index));
        }
        if ("2".equals(s)){
            String inputBookPrice=JOptionPane.showInputDialog(null,"请输入要查找的图书价格");
            for (int i = 0; i <arrayList.size() ; i++) {
                if (arrayList.get(i).price==Integer.parseInt(inputBookPrice)){
                    index=i;
                }
            }
            JOptionPane.showMessageDialog(null,arrayList.get(index));
        }if ("3".equals(s)){
            String inputAuthorName=JOptionPane.showInputDialog(null,"请输入要查找的图书作者");
            for (int i = 0; i <arrayList.size() ; i++) {
                if (arrayList.get(i).authorNmae.equals(inputAuthorName)){
                    index=i;
                }
            }
            JOptionPane.showMessageDialog(null,arrayList.get(index));
        }if ("4".equals(s)){
            String inputBookNum=JOptionPane.showInputDialog(null,"请输入要查找的图书编号");
            for (int i = 0; i <arrayList.size() ; i++) {
                if (arrayList.get(i).bookNum==Integer.parseInt(inputBookNum)){
                    index=i;
                }
            }
            JOptionPane.showMessageDialog(null,arrayList.get(index));
        }


    }
    public static void changeBook(){

    }
    public static void show(){
        StringBuilder s= new StringBuilder();
        for (int i = 0; i <arrayList.size() ; i++) {
             s.append(arrayList.get(i).toString()).append("\n");
        }
        JOptionPane.showMessageDialog(null, s.toString());
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookName='" + bookName + '\'' +
                ", price=" + price +
                ", authorNmae='" + authorNmae + '\'' +
                ", bookNum=" + bookNum +
                '}';
    }
}
